package cn.agriculture.web.controller;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mobile.device.Device;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.agriculture.web.form.CartForm;
import cn.agriculture.web.form.GoodsForm;
import cn.agriculture.web.form.GuestForm;
import cn.agriculture.web.form.UVO;
import cn.agriculture.web.service.CartService;
import cn.agriculture.web.service.GoodsService;
import cn.agriculture.web.service.GuestService;

@Slf4j
@Controller("GuestController")
@RequestMapping("/")
public class GuestController {

	@Autowired
	GuestService guestService;
	
	@Autowired
	GoodsService goodsService;
	
	@Autowired
	CartService cartService;
	
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public String index(Model model, HttpSession session, Device device) {
    	log.info("系统初始化");
    	UVO uvo = new UVO();
    	session.setAttribute("UVO", uvo);
    	GoodsForm goodsForm = new GoodsForm();
    	goodsForm.setType("粮食");
    	model.addAttribute("goodsForm", goodsForm);
    	model.addAttribute("list", goodsService.searchGoodsList(goodsForm));
    	List<CartForm> cartList = new ArrayList<>();
    	model.addAttribute("cartList", cartList);
    	if(device.isNormal()) {
    		return "shop/index";
    	} else {
    		return "mobile/index";
    	}
        
    }
    
    @RequestMapping(value = "initGuestLogin", method = RequestMethod.GET)
    public String initGuestLogin(Model model, Device device) {
    	log.info("客户登录界面初始化");
    	GoodsForm goodsForm = new GoodsForm();
    	goodsForm.setType("粮食");
    	model.addAttribute("goodsForm", goodsForm);
    	List<CartForm> cartList = new ArrayList<>();
    	model.addAttribute("cartList", cartList);
    	GuestForm guestForm = new GuestForm();
    	model.addAttribute("guestForm", guestForm);
    	if(device.isNormal()) {
    		return "shop/login";
    	} else {
    		return "mobile/login";
    	}
    }
    
	@RequestMapping(value = "guestLogin", method = RequestMethod.POST)
    public String guestLogin(Model model, HttpSession session, GuestForm guestForm, Device device) {
		log.info("客户登录，验证客户信息，成功后进入系统");
		GuestForm result = guestService.searchGuest(guestForm);
		if(result != null) {
			UVO uvo = new UVO();
			uvo.setGuestId(result.getGuestId());
			uvo.setGuestName(result.getGuestName());
			uvo.setPassword(guestForm.getPassword());
			uvo.setGender(result.getGender());
			uvo.setAddress(result.getAddress());
			uvo.setEmail(result.getEmail());
			uvo.setMobile(result.getMobile());
			uvo.setQq(result.getQq());
			uvo.setPhone(result.getPhone());
			uvo.setZip(result.getZip());
			session.setAttribute("UVO", uvo);
			GoodsForm goodsForm = new GoodsForm();
	    	goodsForm.setType("粮食");
	    	model.addAttribute("goodsForm", goodsForm);
	    	model.addAttribute("list", goodsService.searchGoodsList(goodsForm));
	    	CartForm cartForm = new CartForm();
	    	cartForm.setGuestId(uvo.getGuestId());
	    	model.addAttribute("cartList", cartService.searchCartList(cartForm));
	    	if(device.isNormal()) {
	    		return "shop/index";
	    	} else {
	    		return "mobile/index";
	    	}
		} else {
			model.addAttribute("message", "用户名或密码错误！");
			GoodsForm goodsForm = new GoodsForm();
	    	goodsForm.setType("粮食");
	    	model.addAttribute("goodsForm", goodsForm);
	    	List<CartForm> cartList = new ArrayList<>();
	    	model.addAttribute("cartList", cartList);
			if(device.isNormal()) {
	    		return "shop/login";
	    	} else {
	    		return "mobile/login";
	    	}
		}
	}
    
    @RequestMapping(value = "initGuestRegister", method = RequestMethod.GET)
    public String initGuestRegister(Model model, Device device) {
    	log.info("客户注册界面初始化");
    	GoodsForm goodsForm = new GoodsForm();
    	goodsForm.setType("粮食");
    	model.addAttribute("goodsForm", goodsForm);
    	List<CartForm> cartList = new ArrayList<>();
    	model.addAttribute("cartList", cartList);
    	GuestForm guestForm = new GuestForm();
    	model.addAttribute("guestForm", guestForm);
    	if(device.isNormal()) {
    		return "shop/register";
    	} else {
    		return "mobile/register";
    	}
    }
    
	@RequestMapping(value = "addGuest", method = RequestMethod.POST)
	public String executeAddGuest(Model model, HttpSession session, @Valid @ModelAttribute("guestForm") GuestForm guestForm, BindingResult results, Device device) throws SQLException {
		if (results.hasErrors()) {
			log.info("内容验证出错");
			GoodsForm goodsForm = new GoodsForm();
	    	goodsForm.setType("粮食");
	    	model.addAttribute("goodsForm", goodsForm);
	    	List<CartForm> cartList = new ArrayList<>();
	    	model.addAttribute("cartList", cartList);
			if(device.isNormal()) {
	    		return "shop/register";
	    	} else {
	    		return "mobile/register";
	    	}
		}
		if(guestForm.getGuestId().length() > 4 && "Guest".equals(guestForm.getGuestId().substring(0, 5))) {
			log.info("ID验证出错");
			model.addAttribute("message", "Guest是系统预留关键字，请避免使用！");
			GoodsForm goodsForm = new GoodsForm();
	    	goodsForm.setType("粮食");
	    	model.addAttribute("goodsForm", goodsForm);
	    	List<CartForm> cartList = new ArrayList<>();
	    	model.addAttribute("cartList", cartList);
			if(device.isNormal()) {
	    		return "shop/register";
	    	} else {
	    		return "mobile/register";
	    	}
		}
		if (!guestForm.getPassword().equals(guestForm.getPasswordConfirm())) {
			log.info("密码验证出错");
			model.addAttribute("message", "密码和密码确认必须一致！");
			GoodsForm goodsForm = new GoodsForm();
	    	goodsForm.setType("粮食");
	    	model.addAttribute("goodsForm", goodsForm);
	    	List<CartForm> cartList = new ArrayList<>();
	    	model.addAttribute("cartList", cartList);
			if(device.isNormal()) {
	    		return "shop/register";
	    	} else {
	    		return "mobile/register";
	    	}
		}
		log.info("添加客户信息");
		guestForm.setUpdateUser(guestForm.getGuestId());
		Date date = new Date();
		SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		guestForm.setUpdateTime(dateformat.format(date));
		boolean result = guestService.addGuest(guestForm);
		if(!result) {
			//throw new SQLException("客户信息添加失败！");
			model.addAttribute("message", "该用户ID已被占用，请更换用户ID！");
			GoodsForm goodsForm = new GoodsForm();
	    	goodsForm.setType("粮食");
	    	model.addAttribute("goodsForm", goodsForm);
	    	List<CartForm> cartList = new ArrayList<>();
	    	model.addAttribute("cartList", cartList);
			if(device.isNormal()) {
	    		return "shop/register";
	    	} else {
	    		return "mobile/register";
	    	}
		}
		UVO uvo = new UVO();
		uvo.setGuestId(guestForm.getGuestId());
		uvo.setGuestName(guestForm.getGuestName());
		uvo.setPassword(guestForm.getPassword());
		uvo.setGender(guestForm.getGender());
		uvo.setAddress(guestForm.getAddress());
		uvo.setEmail(guestForm.getEmail());
		uvo.setMobile(guestForm.getMobile());
		uvo.setQq(guestForm.getQq());
		uvo.setPhone(guestForm.getPhone());
		uvo.setZip(guestForm.getZip());
		session.setAttribute("UVO", uvo);
		
		GoodsForm goodsForm = new GoodsForm();
    	goodsForm.setType("粮食");
    	model.addAttribute("goodsForm", goodsForm);
    	model.addAttribute("list", goodsService.searchGoodsList(goodsForm));
    	CartForm cartForm = new CartForm();
    	cartForm.setGuestId(uvo.getGuestId());
    	model.addAttribute("cartList", cartService.searchCartList(cartForm));
    	if(device.isNormal()) {
    		return "shop/index";
    	} else {
    		return "mobile/index";
    	}
	}
	
    @RequestMapping(value = "initEditGuest", method = RequestMethod.GET)
    public String initEditGuest(Model model, HttpSession session, Device device) {
    	log.info("修改客户信息初始化");
    	GoodsForm goodsForm = new GoodsForm();
    	goodsForm.setType("粮食");
    	model.addAttribute("goodsForm", goodsForm);
    	UVO uvo = (UVO)session.getAttribute("UVO");
    	GuestForm guestForm = new GuestForm();
    	guestForm.setGuestId(uvo.getGuestId());
    	guestForm.setPassword(uvo.getPassword());
    	GuestForm result = guestService.searchGuest(guestForm);
		model.addAttribute("guestForm", result);
		CartForm cartForm = new CartForm();
    	cartForm.setGuestId(uvo.getGuestId());
    	model.addAttribute("cartList", cartService.searchCartList(cartForm));
		if(device.isNormal()) {
    		return "shop/editGuest";
    	} else {
    		return "mobile/editGuest";
    	}
    }
    
	@RequestMapping(value = "editGuest", method = RequestMethod.POST)
	public String executeEditGuest(Model model, HttpSession session, @Valid @ModelAttribute("guestForm") GuestForm guestForm, BindingResult results, Device device) throws SQLException {
		if (results.hasErrors()) {
			log.info("内容验证出错");
			UVO uvo = (UVO)session.getAttribute("UVO");
			GoodsForm goodsForm = new GoodsForm();
	    	goodsForm.setType("粮食");
	    	model.addAttribute("goodsForm", goodsForm);
	    	CartForm cartForm = new CartForm();
	    	cartForm.setGuestId(uvo.getGuestId());
	    	model.addAttribute("cartList", cartService.searchCartList(cartForm));
			if(device.isNormal()) {
	    		return "shop/editGuest";
	    	} else {
	    		return "mobile/editGuest";
	    	}
		}
		if (!guestForm.getPassword().equals(guestForm.getPasswordConfirm())) {
			log.info("密码验证出错");
			model.addAttribute("message", "密码和密码确认必须一致！");
			UVO uvo = (UVO)session.getAttribute("UVO");
			GoodsForm goodsForm = new GoodsForm();
	    	goodsForm.setType("粮食");
	    	model.addAttribute("goodsForm", goodsForm);
	    	CartForm cartForm = new CartForm();
	    	cartForm.setGuestId(uvo.getGuestId());
	    	model.addAttribute("cartList", cartService.searchCartList(cartForm));
			if(device.isNormal()) {
	    		return "shop/editGuest";
	    	} else {
	    		return "mobile/editGuest";
	    	}
		}
		log.info("修改客户信息");
		guestForm.setUpdateUser(guestForm.getGuestId());
		Date date = new Date();
		SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		guestForm.setUpdateTime(dateformat.format(date));
		boolean result = guestService.editGuest(guestForm);
		if(!result) {
			throw new SQLException("客户信息更新失败！");
		}
		UVO uvo = new UVO();
		uvo.setGuestId(guestForm.getGuestId());
		uvo.setGuestName(guestForm.getGuestName());
		uvo.setPassword(guestForm.getPasswordConfirm());
		uvo.setGender(guestForm.getGender());
		uvo.setAddress(guestForm.getAddress());
		uvo.setEmail(guestForm.getEmail());
		uvo.setMobile(guestForm.getMobile());
		uvo.setQq(guestForm.getQq());
		uvo.setPhone(guestForm.getPhone());
		uvo.setZip(guestForm.getZip());
		session.setAttribute("UVO", uvo);
		
		GoodsForm goodsForm = new GoodsForm();
    	goodsForm.setType("粮食");
    	model.addAttribute("goodsForm", goodsForm);
    	model.addAttribute("list", goodsService.searchGoodsList(goodsForm));
    	CartForm cartForm = new CartForm();
    	cartForm.setGuestId(uvo.getGuestId());
    	model.addAttribute("cartList", cartService.searchCartList(cartForm));
    	if(device.isNormal()) {
    		return "shop/index";
    	} else {
    		return "mobile/index";
    	}
	}
}
